package com.tansen.springcould.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.tansen.springcould.api.entities.Dept;
import com.tansen.springcould.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/dept")
public class DeptController {

    @Autowired
    private DeptService service;

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public boolean add(@RequestBody Dept dept) {
        return service.add(dept);
    }

    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @HystrixCommand(fallbackMethod = "processHystrix_Get")
    public Dept get(@PathVariable("id") Long id) {
//        return service.get(id);

        Dept dept =  this.service.get(id);
        if(null == dept)
        {
//            try {
//                Thread.sleep(5000);
//            } catch (InterruptedException e) {
//            }
            throw new RuntimeException("该ID："+id+"没有没有对应的信息");
        }
        return dept;
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public List<Dept> list() {
        return service.list();
    }


    public Dept processHystrix_Get(@PathVariable("id") Long id)
    {
        Dept dept = new Dept().setDeptno(id).setDname("该ID：" + id + "没有没有对应的信息,null--@HystrixCommand").setDb_source("no this database in MySQL");

        return dept;
    }

}
 

